(function () {
    'use strict';

    var http        = require('http'),
        querystring = require('querystring'),
		nodemailer  = require('nodemailer'),
        nreq        = 0,
        port        = 80;

	if (process.argv[2] !== undefined) {
        port = parseInt(process.argv[2], 10);
    }
	
    http.createServer(function (req, res) {
        switch (req.url.toLowerCase()) {
        case '/':
            if (req.headers.cookie === 'ale=28146297') {
                res.writeHead(200, {
                    'Content-Type' : 'text/html',
                    'Cache-Control': 'no-cache, no-store, must-revalidate',
                    'Pragma'       : 'no-cache',
                    'Expires'      : '0'
                });
                res.end('<!DOCTYPE html><html><body><h1 id=mingo></h1><script>document.getElementById("mingo").innerHTML=document.cookie;</script><a href="/logout">salir...</a></body></html>');
            } else {
                res.writeHead(302, {
                    'Location' : '/login'
                });
                res.end();
            }
            break;
        case '/login':
            if (req.headers.cookie === 'ale=28146297') {
                res.writeHead(302, {
                    'Location' : '/'
                });
                res.end();
            } else {
                res.writeHead(200, {
                    'Content-Type' : 'text/html',
                    'Cache-Control': 'no-cache, no-store, must-revalidate',
                    'Pragma'       : 'no-cache',
                    'Expires'      : '0'
                });
                res.write('<!DOCTYPE html>');
                res.write('<html><body><form name="input" action="loginx" method="POST">');
                res.write('usuario <input type="text" name="usr" placeholder="usuario o mail"><br>');
                res.write('password <input type="password" name="pwd" placeholder="palabra secreta"><br>');
                res.write('<input type="submit" value="Ok">');
                res.write('</form>');
                res.write('<br>');
                res.write('<a href="/forgpwd">olvid&eacute mi password<a>');
                res.write('<br>');
                res.write('<a href="/newusr">no tengo usuario<a>');
                res.write('</body></html>');
                res.end();
            }
            break;
        case '/loginx':
            if (req.method.toUpperCase() === 'POST') {
                var body = '';
                req.on('data', function (data) {
                    body += data;
                    if (body.length > 1024) {
                        res.writeHead(413, {
                            'Content-Type' : 'text/html'
                        });
                        res.end('<!DOCTYPE html><html><body><h1>413</h1><p>la entidad es demasiado larga flac@!</p></body></html>');
                        req.connection.destroy();
                    }
                });
                req.on('end', function () {
                    var post = querystring.parse(body);
                    if ((post.usr === 'avarela') && (post.pwd === '123456')) {
                        res.writeHead(302, {
                            'Location'  : '/',
                            'Set-Cookie': 'ale=28146297'
                        });
                        res.end();
                    } else {
                        res.writeHead(302, {
                            'Location' : '/login'
                        });
                        res.end();
                    }
                });
            } else {
                res.writeHead(405, {'Content-Type' : 'text/html'});
                res.end('<!DOCTYPE html><html><body><h1>405</h1><p>m&eacutetodo no permitido flac@!</p></body></html>');
            }
            break;
        case '/logout':
            res.writeHead(200, {
                'Content-Type': 'text/html',
                'Set-Cookie'  : 'ale=; expires=Thu, 01 Jan 1970 00:00:01 GMT'
            });
            res.end('<!DOCTYPE html><html><body><h1>Chau!</h1><a href="/login">volver al login...</a></body></html>');
            break;
        case '/favicon.ico':
            res.writeHead(200, {'Content-Type': 'image/x-icon'});
            res.end();
            break;
        default:
            res.writeHead(404, {'Content-Type': 'text/html'});
            res.end('<!DOCTYPE html><html><body><h1>404</h1><p>no encontr&eacute lo que buscabas!</p></body></html>');
            break;
        }
    }).listen(port);

    console.log('servidor @ http://localhost:' + port);
}());
